#include "Mesh.h"

#include <GL/gl.h>

using namespace std;

Mesh::Mesh()
{
	color[0] = color[1] = color[2] = 0.5;
}

void Mesh::addFace(const Vec3& v1, const Vec3& v2, const Vec3 & v3, const Vec3 &norm) {
    faces.push_back(Face(v1, v2, v3, norm));
}


void Mesh::setColor(float r, float g, float b)
{
	color[0] = r;
	color[1] = g;
	color[2] = b;
}


void Mesh::render() {
    glPushAttrib(GL_ALL_ATTRIB_BITS);
    glPushMatrix();

	for(list<Face>::iterator iter = faces.begin(); iter != faces.end(); iter++){
		glBegin(GL_TRIANGLES);
			glColor3fv(color);
			glNormal3f(iter->normal.x, iter->normal.y, iter->normal.z);
			glVertex3f(iter->v1.x, iter->v1.y, iter->v1.z);
			glVertex3f(iter->v2.x, iter->v2.y, iter->v2.z);
			glVertex3f(iter->v3.x, iter->v3.y, iter->v3.z);
		glEnd();
	}

    glPopMatrix();
    glPopAttrib();
}


Mesh::Face::Face(const Vec3& _v1, const Vec3& _v2, const Vec3 & _v3, const Vec3 &norm) {
    v1 = _v1;
    v2 = _v2;
    v3 = _v3;
    normal = norm;
}
